package com.libo.server.store.impl;

import com.libo.bean.Environment;
import com.libo.server.store.Store;
import com.libo.utils.DateTimeUtils;
import com.libo.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;

public class StoreImpl implements Store {
    @Override
    public void store(List<Environment> environments) {

        //获取数据库连接池对象
        Connection connection = JDBCUtils.getConnection();
        PreparedStatement preparedStatement = null;
//        try {
//            preparedStatement = connection.prepareStatement("insert into e_detail_1 (name,send_id,smart_id,area_model_id,sensor_address,sensor_count,instruction_designation,status,environment_data,gather_time) values (?,?,?,?,?,?,?,?,?,?)");
//        } catch (SQLException e) {
//            throw new RuntimeException(e);
//        }
        //某一次事务中第几次执行统计
        int count = 0;
        //入库数据总条数
        int saveCount = 0;

        //上一条数据采集时间对应日期
        int preDay = -1;
        //当前遍历到的数据对应日期
        int curryDay = -1;


        try {
            //开启事务【手动提交】
            connection.setAutoCommit(false);

            for (Environment environment : environments) {

                //记录当前时间
                curryDay = environment.getGatherTime().getDayOfMonth();

                //何时给PS对象赋值，  何时对PS对象进行修改
                if(preDay == -1 || preDay != curryDay){

                    if(preDay != -1){
                        preparedStatement.executeBatch();
                        connection.commit();
                        saveCount += count;
                        count = 0;
                        preparedStatement.close();
                    }
                    preparedStatement = connection.prepareStatement("insert into e_detail_"+curryDay+" (name,send_id,smart_id,area_model_id,sensor_address,sensor_count,instruction_designation,status,environment_data,gather_time) values (?,?,?,?,?,?,?,?,?,?)");

                    System.out.println(curryDay);
                    System.out.println();
                    System.out.println("执行SQL："+"insert into e_detail_\"+curryDay+\" (name,send_id,smart_id,area_model_id,sensor_address,sensor_count,instruction_designation,status,environment_data,gather_time) values (?,?,?,?,?,?,?,?,?,?)");

                }


                preparedStatement.setString(1, environment.getName());
                preparedStatement.setString(2, environment.getSendId());
                preparedStatement.setString(3, environment.getSmartId());
                preparedStatement.setString(4, environment.getAreaModelId());
                preparedStatement.setString(5, environment.getSensorAddress());
                preparedStatement.setInt(6, environment.getSensorCount());
                preparedStatement.setString(7, environment.getInstructionDesignation());
                preparedStatement.setInt(8, environment.getStatus());
                preparedStatement.setFloat(9, environment.getEnvironmentData());
                preparedStatement.setTimestamp(10,new Timestamp(DateTimeUtils.localDateTimeCoverTime(environment.getGatherTime())));

                //批处理
                preparedStatement.addBatch();
                count++;
                if (count % 3 == 0) {
                    preparedStatement.executeBatch();
                    connection.commit();
                    saveCount += count;
                    count = 0;

                }

                preDay = curryDay;

            }
            //保险  再次提交
            preparedStatement.executeBatch();
            connection.commit();
            saveCount += count;

            System.out.println("提交了: 第"+saveCount+"数据，成功入库");

            System.out.println("共入库：" + saveCount+"条数据");

        } catch (SQLException e) {
            try {
                connection.rollback();
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
            throw new RuntimeException(e);
        }finally {
            JDBCUtils.close(connection,preparedStatement);
        }
    }
}
